﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Model
{
    public class ApiServiceContext
    {
        public ApiHttpRequest apiHttpReq { get; set; }

        public string merKey { get; set; }
        /// <summary>
        /// base64解密后data
        /// </summary>
        public string decData { get; set; }
        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public long CurrUserId { get; set; }
        public ApiHttpResponse apiHttpResp { get; set; }

        public HttpResponseMessage ReturnResp()
        {
            LogHelper.Info("请求响应1：" + apiHttpResp.data);
            //data内容base64
            if (!string.IsNullOrEmpty(apiHttpResp.data))
                apiHttpResp.data = Utils.EncodeBase64(apiHttpResp.data);
            //md5签名
            string signData = string.Format("data={0}&message={1}&retcode={2}&key={3}", apiHttpResp.data, apiHttpResp.message, (int)apiHttpResp.retCode, merKey);
            string sign = Utils.Md5Encrypt(signData, Encoding.UTF8);
            apiHttpResp.sign = sign;

            string result = JsonHelper.Serialize(apiHttpResp);
            LogHelper.Info("请求响应2：" + result);
            HttpResponseMessage resp = new HttpResponseMessage { Content = new StringContent(result, Encoding.GetEncoding("UTF-8"), "application/json") };
            return resp;
        }
    }
}
